<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\GoodsPostRequest;
use App\Http\Controllers\Controller;
use DB;


class GoodsController extends Controller
{
    public function getIndex(){
        //解析模板
        return view('admin.goods.index');
    }
    
    //商品添加
    public function getAdd(){
        //获取分类列表
        $data = DB::table('hm_cate')
                ->orderBy('path')
                ->get();
        //修改分类名的规格
        foreach($data as $k => $v){
            //获取path
            $path = $v->path;
            //计算分类级别
            $num = explode(',',$path);
            //拼接分类名
            $catename = str_repeat('|---',count($num)).$v->catename;
            //修改传递参数名
            $v->catename = $catename;
        }
        //解析模板
        return view('admin.goods.add',['goods' => $data]);
    }

    //上传文件检查后缀,重命名
    static public function file($request,$fname){
        //检测是否上传文件
        if($request->file($fname)){
            //上传文件
            $file = $request->file($fname);
            //获取上传文件后缀
            $extends = $file->getClientOriginalExtension();
            //上传图片命名
            $name = md5(date(time()).rand(1,99999)).'.'.$extends;
            //移动文件
            $a = $file->move('./public/admin/goods/upload',$name);
            //拼接商品文件信息
            $filename = '/public/admin/goods/upload/'.$name;
            return $filename;
        }else{
            $filename = '/public/admin/goods/upload/default.jpg';
            return $filename;
        }
    }

    //商品插入数据库
    public function postInsert(GoodsPostRequest $request){
    //public function postInsert(Request $request){
        //获取商品主要信息
        $data = $request
             ->only('cid','pic2','pic3','goodsname','price','goodscolor','goodssize','count','year','month','day','goodsinfo');      
        //检测是否上传文件,上传文件检查后缀,重命名       
        $goodspic = self::file($request,'goodspic');
        $pic2 = self::file($request,'pic2');
        $pic3 = self::file($request,'pic3');
        //拼接商品上架时间
            $year = $data['year'];
            $month = $data['month'];
            $day = $data['day'];
            if($year && $month && $day){
                //如果用户没有输入时间,拼接时间
                $time = mktime(0,0,0,$month,$day,$year);
            }else{
                //如果没有输入,设置当前时间为商品上架时间
                $time = time();
            }
        //拼接商品主要信息
        $goods = [
                    'cid'=>$data['cid'],'goodsname'=>$data['goodsname'],'price'=>$data['price'],
                    'count'=>$data['count'],'goodspic'=>$goodspic
                ];
        //插入商品数据到数据库商品表
        $gid = DB::table('hm_goods')->insertGetId($goods);

        //凭借商品详细信息
        $goodsdes = [
                        'gid'=>$gid,'goodssize'=>$data['goodssize'],'goodscolor'=>$data['goodscolor'],
                        'goodsinfo'=>$data['goodsinfo'],'time'=>$time,'pic2'=>$pic2,'pic3'=>$pic3
                    ];
        //输入商品详情到数据库商品详情表
        $status2 = DB::table('hm_goods_des')->insertGetId($goodsdes);
        //判断数据库插入情况
        if($gid && $status2){
            //商品添加成功默认添加一条商品评论
            $status = DB::table('hm_comments')->insert(['gid'=>$gid]);
            //数据插入成功，直接跳转，输出成功信息
            return redirect('/admin/goods/list')->with('success','商品添加成功');
        }else{
            //输入插入失败，返回，输出失败信息
            return back()->with('error','商品添加失败');
        }

    }
    //商品列表
    public function getList(Request $request){
        //获取查询信息
        $search = $request->input('search');
        //显示记录条数
        $num = $request->input('num',10);
        //判断是否输入查询信息
        if($search){
            //查询信息
            $goodsinfo = DB::table('hm_goods')
            ->where('goodsname','like','%'.$search.'%')
            ->orderBy('id','asc')
            ->paginate($num);
        }else{
            //获取数据
            $goodsinfo = DB::table('hm_goods')->orderBy('cid')->paginate($num);
            //dd($goodsinfo);
            //商品所属分类
            foreach($goodsinfo as $v){
                $cate = DB::table('hm_cate')
                ->where('id',$v->cid)
                ->value('catename');
                //修改分类id为对应名字
                $v->cid = $cate;
            }
        }
        $data = $request->all();
        //传递数据,解析模板
        return view('admin.goods.list',['goodsinfo'=>$goodsinfo,'data'=>$data]);
    }

    //商品编辑
    public function getEdit(Request $request){
        //获取要编辑的id
        $id = $request->only('id')['id'];
        //获取用户数据
        $goodsinfo = DB::table('hm_goods')
            ->where('hm_goods.id',$id)
            ->join('hm_goods_des','hm_goods.id','=','hm_goods_des.gid')
            ->select('hm_goods.id','goodspic','goodsname','price','count','goodssize','goodscolor','goodsinfo','time')
            ->get();
        //判断结果
        if(!$goodsinfo){
            return back()->with('danger','商品信息获取失败');
        }
        //商品上架时间
        $timestamp = $goodsinfo[0]->time;
        $time['year'] = date('Y',$timestamp);
        $time['month'] = date('m',$timestamp);
        $time['day'] = date('d',$timestamp);

        //dd($goodsinfo[0]);
        //解析模板
        return view('admin.goods.edit',['goodsinfo'=>$goodsinfo[0],'time'=>$time]);
    }
    //商品信息更新
    public function postUpdate(Request $request){
        //获取修改信息
        $data = $request
             ->only('id','goodsname','price','goodscolor','pic','goodssize','count','year','month','day','goodsinfo');      
        //判断是否上传文件
        if($request->file('goodspic')){
            //上传文件
            $file = $request->file('goodspic');
            //获取上传文件后缀
            $extends = $file->getClientOriginalExtension();
            //上传图片命名
            $name = md5(date(time()).rand(1,99999)).'.'.$extends;
            //移动文件
            $a = $file->move('./public/admin/goods/upload',$name);
            //拼接商品文件信息
            $filename = '/public/admin/goods/upload/'.$name;
            
        }else{
            $filename = $data['pic'];
        }
        //拼接商品上架时间
        $year = $data['year'];
        $month = $data['month'];
        $day = $data['day'];
        $time = mktime(0,0,0,$month,$day,$year);

        $goods = [
                    'goodsname'=>$data['goodsname'],'price'=>$data['price'],
                    'count'=>$data['count'],'goodspic'=>$filename,
                    'goodssize'=>$data['goodssize'],'goodscolor'=>$data['goodscolor'],
                     'goodsinfo'=>$data['goodsinfo'],'time'=>$time
                ];
        //更新数据
        $status = DB::table('hm_goods')
                ->join('hm_goods_des','hm_goods.id','=','hm_goods_des.gid')
                ->where('hm_goods.id',$data['id'])
                ->update($goods);

        if($status){
            //数据插入成功，直接跳转，输出成功信息
            return redirect('/admin/goods/list')->with('success','商品修改成功');
        }else{
            //输入插入失败，返回，输出失败信息
            return redirect('/admin/goods/list')->with('error','商品修改失败');
        }
    }
    //删除ajax请求的id数据
    public function getDelete(Request $request){
        //获取ajax发送的id
        $id = $request->all();
        //删除商品
        $status = DB::table('hm_goods')->where('id',$id)->delete();
        //判断是否删除
        if($status){
            //成功返回1
            return 1;
        }else{
            return 0;
        }

    }
}
